from modules import music
from common import *
from common import ParameterEntity, PlayEntity, SearchFilter, MemberEntity
from businessrules import SongBiz,RatingBiz
from dataaccess import SongData, ParameterData, PlayData, MemberData, RatingData

def search():
    """/radio/music/search
    music library search interface"""
    resp = dict()

    sf = SearchFilter.ComplexFilter(SearchFilter.OperatorType.AND)

    title = request.vars.get("Song")
    if title != None and title.strip() != "":
        songFilter = SearchFilter.SimpleFilter(SongEntity.SongEntity.ColumnNames.name,
            SearchFilter.ConditionType.Contain, title)
        sf.Filters.append(songFilter)

    album = request.vars.get("Album")
    if album != None and album.strip() != "": 
        albumFilter = SearchFilter.SimpleFilter(SongEntity.SongEntity.ColumnNames.albumName,
            SearchFilter.ConditionType.Contain, album)
        sf.Filters.append(albumFilter)
    
    artist = request.vars.get("Artist")
    if artist != None and artist.strip() != "":
        artistFilter =  SearchFilter.SimpleFilter(SongEntity.SongEntity.ColumnNames.artistName,
            SearchFilter.ConditionType.Contain, artist)
        sf.Filters.append(artistFilter)
        
    genre = request.vars.get("genre")
    if genre != None and genre.strip() != "":
        genreFilter = SearchFilter.SimpleFilter(SongEntity.SongEntity.ColumnNames.genre,
                SearchFilter.ConditionType.Equal, genre)
        sf.Filters.append(genreFilter)
    
    AllSongs = SongData.SongData().SelectSongByFilter(sf)
    resp["all_songs"] = AllSongs

    sf = SearchFilter.SimpleFilter(ParameterEntity.ParameterEntity.ColumnNames.paramType, SearchFilter.ConditionType.Equal, utility.ParameterGroup.Genre)
    genreRows = ParameterData.ParameterData().SelectParameterByFilter(sf)
    genres = {}
    for parameterEntity in genreRows:
        genres[parameterEntity.ParamID] = parameterEntity.ParamName
    resp["genres"] = genres
    return resp 

def listen():
    """/radio/music/listen
    get the current song being played and the dj who played it"""
    play = PlayData.PlayData().SelectLastPlay()
    if play:
        song = SongData.SongData().SelectSongByID(play.SongID)
        dj = MemberData.MemberData().SelectMemberByID(play.DJ)
        rating = RatingData.RatingData().SelectRatingByID(session.username, song.SongID)
        if(request.env.request_method == "POST"):
          print("gote here")
          rate(play.SongID)
    return dict(song=song, dj=dj, rating=rating)

def rate(song_id):
    """/radio/music/rate
    rates the song with song_id"""
    print("in rating method")
    rating = request.vars.get("rating")
    ratingEntity = RatingEntity.RatingEntity(memberID=session.username, songID=song_id, score=rating)
    RatingBiz.RatingBiz().InsertRating(ratingEntity)

def songs():
    album_id = request.args(0)
    return music.songs(album_id)

def add_song():
    song = SongEntity.SongEntity(albumID=request.vars.get("album_id"),
                                 songID=None,
                                 name=request.vars.get("title"),
                                 visibility=request.vars.get("visibility"))
    SongBiz.SongBiz().InsertSong(song)
    redirect("/radio/music/songs/%s" % song.AlbumID)

def delete_song():
    song_id = request.vars.get("song_id")
    album_id = request.vars.get("album_id")
    song = SongData.SongData().DeleteSong(song_id)
    redirect("/radio/music/songs/%s" % album_id)
